import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:oi_ai_tool/common/IconFontIcons.dart';
import 'package:oi_ai_tool/config/ai_source.dart';
import 'package:oi_ai_tool/model/request_token.dart';
import 'package:oi_ai_tool/route/oi_route.dart';
import 'package:oi_ai_tool/util/oi_logger.dart';
import 'package:oi_ai_tool/util/storage.dart';
import 'package:oi_ai_tool/widgets/toast.dart';

class OiTokenController extends GetxController {
  GlobalKey<FormState>? formKey;
  RequestToken requestToken = RequestToken();
  bool showPwd = false;
  bool isBaidu = true;
  final TextEditingController apikeyController = TextEditingController();
  final TextEditingController secretKeyController = TextEditingController();
  @override
  onReady() async {
    await initeToken();
  }

  get aiSource {
    return requestToken.aiSource ?? "";
  }

  initeToken() async {
    EasyLoading.show();
    String reqToken = await OiStorage.get(StoreageKey.TOKEN_KEY);
    Map<String, dynamic> json = jsonDecode(reqToken) as Map<String, dynamic>;

    requestToken = RequestToken.formJson(json);
    apikeyController.text = requestToken.apiKey!;
      secretKeyController.text = requestToken.secretKey!;
  
    onSelectChanged(requestToken.aiSource);
    EasyLoading.dismiss();
    update();
    }

  @override
  onClose() {
    OiLogger.info(msg: "token onClose");
    super.onClose();
    apikeyController.dispose();
    secretKeyController.dispose();
    // removeFormKey();
  }

  onPressed() {
    bool validate = formKey!.currentState!.validate();
    if (validate) {
      formKey!.currentState!.save();
      OiLogger.info(msg: requestToken.toString());
      OiToast.msg("提交表单成功");
      OiStorage.set(StoreageKey.TOKEN_KEY, jsonEncode(requestToken.toJson()));
      formKey!.currentState!.reset();
      onCancel();
    }
  }

  void onCancel() {
    OiRoute.off(Routes.Initials);
  }

  void removeFormKey() {
    formKey!.currentState!.dispose();
  }

  void onSelectChanged(String? value) {
    if (value!.isNotEmpty &&
        AiSource.CHATGPT.type.toString().compareTo(value) == 0) {
      apikeyController.text = "";
      requestToken.apiKey = "";
      isBaidu = false;
    } else {
      apikeyController.text = requestToken.apiKey!;
      isBaidu = true;
    }
    update();
  }

  Widget onShowPwd() {
    if (showPwd) {
      return const Icon(IconFontIcons.iconEyeOpen);
    } else {
      return const Icon(IconFontIcons.iconEyeClose);
    }
  }

  changeShow() {
    showPwd = !showPwd;
    update();
  }

  String? defaultNotEmpetyValidator(String? value) {
    String emptyTipStr = "内容不能为空";
    if (value == null) {
      return emptyTipStr;
    }
    String? val = value ?? value.trim();
    if (val.isEmpty) {
      return emptyTipStr;
    }

    return null;
  }

  String? defaultNotSelectEmpetyValidator(String? value) {
    String emptyTipStr = "请选择对话 ai";
    if (value == null) {
      return emptyTipStr;
    }
    String? val = value ?? value.trim();
    if (val.isEmpty) {
      return emptyTipStr;
    }

    return null;
  }
}
